﻿Imports VSModel
Imports System.Linq
Imports System.Data
Imports Microsoft.Data.Extensions
Imports System.Collections.Generic
Imports System.Text
Imports System.Data.Common
Imports System.Data.SqlClient
Imports Cls_Common


Partial Class Control_WhatCookToDay_ContentHome
    Inherits System.Web.UI.UserControl


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            If Not Page.RouteData.Values("MenuId") Is Nothing Then
                Dim iMenuId As Integer = Page.RouteData.Values("MenuId")
                LoadItemIndex(iMenuId)
            End If
        End If
    End Sub

    Sub LoadItemIndex(ByVal iMenuid As Integer)
        'Load data menu every day & knor Coorner
        Using dta As New VSEntities
            hplTitle.Text = "Thực đơn hằng ngày"
            hplTitle.NavigateUrl = MainDomain & "thuc-don-hang-ngay-139.html"

            'Get 1 record moi nhatof object Formula
            Dim onl = (From f In dta.formulae Where f.IsHot = True And f.StatusId = 3 And f.PublishedDate <= Now And _
                       (f.eEffectiveDate >= Now Or f.eEffectiveDate Is Nothing) Order By f.PublishedDate Descending Select _
                       New With {f.Id, f.Image, f.Title, f.Description, f.QueryString, .QueryStringFormulaType = f.FormulaType.QueryString, f.FormulaTypeId}).FirstOrDefault
            If (Not onl Is Nothing) Then
                hplImg.NavigateUrl = MainDomain & Page.RouteData.Values("MenuId") & "-" & strRewriteWhatCookToday & "/" & onl.QueryStringFormulaType & "/" & onl.QueryString & "-" & onl.Id & ".html"
                hplImg.Text = "<img src=""" & CookedURLImage("280-210-", onl.Image.ToString) & """ class=""imgLage_left"" />"

                hplNewsTitle.NavigateUrl = MainDomain & Page.RouteData.Values("MenuId") & "-" & strRewriteWhatCookToday & "/" & onl.QueryStringFormulaType & "/" & onl.QueryString & "-" & onl.Id & ".html"
                hplNewsTitle.Text = onl.Title

                hplNewsDesc.NavigateUrl = MainDomain & Page.RouteData.Values("MenuId") & "-" & strRewriteWhatCookToday & "/" & onl.QueryStringFormulaType & "/" & onl.QueryString & "-" & onl.Id & ".html"
                hplNewsDesc.Text = onl.Description

                'Load 3 record moi nhat tiep theo
                Dim onl3 = (From f In dta.formulae Where f.IsHot = False And f.StatusId = 3 And f.PublishedDate <= Now And _
                       (f.eEffectiveDate >= Now Or f.eEffectiveDate Is Nothing) Order By f.PublishedDate Descending Select _
                       New With {f.Id, f.Image, f.Title, f.Description, f.QueryString, .QueryStringFormulaType = f.FormulaType.QueryString, f.FormulaTypeId}).Skip(0).Take(3).ToList
                With rptMenuDayRelated
                    .DataSource = onl3
                    .DataBind()
                End With
            End If

        End Using


        Dim cls As New clsDA_HomeIndex
        Dim dtsSrc As DataSet = cls.Select_Content_HomeOriginal(iMenuid)

        If Not dtsSrc Is Nothing AndAlso dtsSrc.Tables.Count > 0 Then
            If dtsSrc.Tables.Count > 1 Then
                ViewState("vSubMenu") = dtsSrc.Tables(1)
            Else
                ViewState("vSubMenu") = Nothing
            End If
            If dtsSrc.Tables.Count > 2 Then
                ViewState("vContentMenu") = dtsSrc.Tables(2)
            Else
                ViewState("vContentMenu") = Nothing
            End If
        Else
            ViewState("vSubMenu") = dtsSrc.Tables(1)
            ViewState("vContentMenu") = dtsSrc.Tables(2)
        End If

        If Not dtsSrc.Tables(0) Is Nothing Then
            Dim dvContentHome As New DataView
            dvContentHome = dtsSrc.Tables(0).DefaultView
            'Get cac menu thuoc  menu nau gi hom nay --> tru di menu thuc don hang ngay va goc knor
            dvContentHome.RowFilter = "MenuId <> 139" ' & idDailyMenu & "  and MenuId <> " & idKnorCooner

            With rptNewsHome
                .DataSource = dvContentHome
                .DataBind()
            End With
        End If
        ViewState("vSubMenu") = Nothing
        ViewState("vContentMenu") = Nothing

        cls.Destructor()
        cls = Nothing

    End Sub
    'rptMenuDayRelated_ItemDataBound
    Protected Sub rptMenuDayRelated_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptMenuDayRelated.ItemDataBound

        Dim row As Object = CType(e.Item.DataItem, Object)
        Dim hplImgNewsRelated As HyperLink = CType(e.Item.FindControl("hplImgNewsRelated"), HyperLink)
        hplImgNewsRelated.NavigateUrl = MainDomain & Page.RouteData.Values("MenuId") & "-" & strRewriteWhatCookToday & "/" & row.QueryStringFormulaType & "/" & row.QueryString & "-" & row.Id & ".html"
        hplImgNewsRelated.Text = "<img src='" & CookedURLImage("88-52-", row.Image.ToString) & "' />"

        Dim hplTitle As HyperLink = CType(e.Item.FindControl("hplTitle"), HyperLink)
        hplTitle.NavigateUrl = MainDomain & Page.RouteData.Values("MenuId") & "-" & strRewriteWhatCookToday & "/" & row.QueryStringFormulaType & "/" & row.QueryString & "-" & row.Id & ".html"
        hplTitle.Text = row.Title.ToString

    End Sub

    'Bind du lieu tin nau gi hom nay len rptNewsHome
    Protected Sub rptNewsHome_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptNewsHome.ItemDataBound
        Dim row As DataRowView = CType(e.Item.DataItem, DataRowView)

        'Dim lblTitle As Label = CType(e.Item.FindControl("lblTitle"), Label)
        'lblTitle.CssClass = "leftTitleBarBg"

        Dim hplTitle As HyperLink = CType(e.Item.FindControl("hplTitle"), HyperLink)
        hplTitle.NavigateUrl = MainDomain & row("QueryString") & "-" & row("MenuId") & ".html"
        hplTitle.Text = row("Title")

        If (Not row("NavigateUrl") Is Nothing AndAlso row("NavigateUrl").ToString <> "") Then
            hplTitle.NavigateUrl = MainDomain & row("NavigateUrl")
        Else
            hplTitle.NavigateUrl = MainDomain & row("QueryString") & "-" & row("MenuId") & ".html"
        End If

        'Load cac sub menu cua menu
        'Dim lblSubItem As Label = CType(e.Item.FindControl("lblSubItem"), Label)
        'lblSubItem.Text = LoadSubItem(CType(row("MenuId"), Integer))

        'Load content of menu
        Dim rptNews As Repeater = CType(e.Item.FindControl("rptNews"), Repeater)
        Dim dtContentMenu As DataTable = CType(ViewState("vContentMenu"), DataTable)
        Dim dvContentMenu As New DataView
        dvContentMenu = dtContentMenu.DefaultView

        'take first row
        dvContentMenu.RowFilter = "ParentId=" & row("MenuID")
        'take first row
        If Not dvContentMenu Is Nothing AndAlso dvContentMenu.Count > 0 Then
            Dim dtFirstContent As DataTable = dtContentMenu.Clone
            Dim dr As DataRow = dtFirstContent.NewRow
            dr("Id") = dvContentMenu(0).Row("Id").ToString
            dr("Title") = dvContentMenu(0).Row("Title").ToString
            dr("Description") = dvContentMenu(0).Row("Description").ToString
            dr("Image") = dvContentMenu(0).Row("Image").ToString
            dr("Like") = dvContentMenu(0).Row("Like")
            dr("QueryString") = dvContentMenu(0).Row("QueryString").ToString
            dr("PublishedDate") = dvContentMenu(0).Row("PublishedDate")
            dr("ParentId") = dvContentMenu(0).Row("ParentId")
            dr("TypeId") = dvContentMenu(0).Row("TypeId")
            dr("IsHot") = dvContentMenu(0).Row("IsHot")
            dtFirstContent.Rows.Add(dr)
            dtFirstContent.AcceptChanges()
            rptNews.DataSource = dtFirstContent
            rptNews.DataBind()
        End If
    End Sub

    'Load sub menu cua menu item
    Function LoadSubItem(ByVal imenuId As Integer) As String
        Dim sKQ As String = ""
        Dim dtSubMenu As DataTable = CType(ViewState("vSubMenu"), DataTable)
        If (Not dtSubMenu Is Nothing AndAlso dtSubMenu.Rows.Count > 0) Then
            Dim dr() As DataRow = dtSubMenu.Select("ParentId=" & imenuId)


            For i As Integer = 0 To dr.Length - 1
                If (i = 0) Then
                    sKQ &= "<a href=""" & dr(i)("QueryString") & "-" & dr(i)("MenuId") & "c.html""  class=""categoryLinkBold"">" & dr(i)("Title") & "</a>"
                Else
                    sKQ &= "<a href=""" & dr(i)("QueryString") & "-" & dr(i)("MenuId") & "c.html""  class=""categoryLinkNormal"">" & dr(i)("Title") & "</a>"
                End If
            Next
            dr = Nothing
        Else
            sKQ = ""
        End If
        dtSubMenu = Nothing
        Return sKQ
    End Function
    'rptNews_ItemDataBound
    Protected Sub rptNews_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs)

        Dim row As DataRowView = CType(e.Item.DataItem, DataRowView)
        Dim hplImg As HyperLink = CType(e.Item.FindControl("hplImg"), HyperLink)
        hplImg.NavigateUrl = MainDomain & row("QueryString") & "-" & row("ParentId") & "-" & row("Id") & "sss.html"
        hplImg.Text = "<img src=""" & CookedURLImage("280-210-", row("Image").ToString) & """ class=""imgLage_left"" />"

        'hplNewsTitle
        Dim hplNewsTitle As HyperLink = CType(e.Item.FindControl("hplNewsTitle"), HyperLink)
        hplNewsTitle.NavigateUrl = MainDomain & row("QueryString") & "-" & row("ParentId") & "-" & row("Id") & "sss.html" 'MainDomain & row("ParentId") & "/" & row("QueryString") & "-" & row("Id") & arrTypeLink(row("TypeId")) & ".html"
        hplNewsTitle.Text = row("Title")

        Dim hplNewsDesc As HyperLink = CType(e.Item.FindControl("hplNewsDesc"), HyperLink)
        hplNewsDesc.NavigateUrl = MainDomain & row("QueryString") & "-" & row("ParentId") & "-" & row("Id") & "sss.html" ' MainDomain & row("ParentId") & "/" & row("QueryString") & "-" & row("Id") & arrTypeLink(row("TypeId")) & ".html"
        hplNewsDesc.Text = row("Description")

        Dim rptNewsRelated As Repeater = CType(e.Item.FindControl("rptNewsRelated"), Repeater)

        'Load content of menu
        Dim dtContentMenu As DataTable = CType(ViewState("vContentMenu"), DataTable)
        Dim dvContentMenu As New DataView
        dvContentMenu = dtContentMenu.DefaultView

        'take four row
        dvContentMenu.RowFilter = "ParentId=" & row("ParentId") & " and Id <> " & row("Id")
        rptNewsRelated.DataSource = dvContentMenu
        rptNewsRelated.DataBind()

    End Sub
    'rptNewsRelated_ItemDataBound
    Protected Sub rptNewsRelated_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs)
        Dim row As DataRowView = CType(e.Item.DataItem, DataRowView)
        Dim hplImgNewsRelated As HyperLink = CType(e.Item.FindControl("hplImgNewsRelated"), HyperLink)
        hplImgNewsRelated.NavigateUrl = MainDomain & row("QueryString") & "-" & row("ParentId") & "-" & row("Id") & "sss.html"  'MainDomain & row("ParentId") & "/" & row("QueryString") & "-" & row("Id") & arrTypeLink(row("TypeId")) & ".html"
        hplImgNewsRelated.Text = "<img src='" & CookedURLImage("88-52-", row("Image").ToString) & "' />"

        'Dim hplplaySmall As HyperLink = CType(e.Item.FindControl("hplplaySmall"), HyperLink)
        'hplplaySmall.NavigateUrl = "~/" & row("ParentId") & "/" & row("QueryString") & "-" & row("Id") & arrTypeLink(row("TypeId")) & ".html"

        Dim hplTitle As HyperLink = CType(e.Item.FindControl("hplTitle"), HyperLink)
        hplTitle.NavigateUrl = MainDomain & row("QueryString") & "-" & row("ParentId") & "-" & row("Id") & "sss.html" ' MainDomain & row("ParentId") & "/" & row("QueryString") & "-" & row("Id") & arrTypeLink(row("TypeId")) & ".html"
        hplTitle.Text = row("Title")
    End Sub

    Protected Sub lnkLoginModal_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkLoginModal.Init
        lnkLoginModal.CssClass = "thickbox"
        lnkLoginModal.NavigateUrl = "~/Page/Member/Login.aspx?keepThis=true&TB_iframe=true&height=341&width=537&modal=true"

    End Sub
    
End Class
